package com.xiyue.lenovo.alertdialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TableLayout;

public class MainActivity extends Activity {
    private EditText show;
    private String[] items=new String[]{
            "疯狂Java讲义",
            "疯狂Ajax讲义",
            "轻量级JavaEE企业实战",
            "疯狂Android讲义"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = (EditText) findViewById(R.id.show);
    }
//1.显示提示消息的对话框
    public void simple(View source) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
        //设置对话框标题
        .setTitle("简单对话框")
        //设置图标
        .setIcon(R.drawable.tools)
        .setMessage("对话框的测试内容\n第二行内容");
        //为AlertDialog.Builder添加"确定"按钮
        setPositiveButton(builder);
        //为AlertDialog.Builder添加“取消”按钮
        setNegativeButton(builder)
        .create()
        .show();
    }
//2.简单列表项对话框
    public void simpleList(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                //设置对话框标题
                .setTitle("简单列表项对话框")
                //设置图标
                .setIcon(R.drawable.tools)
                //设置简单的列表项内容
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        show.setText("您选中了《"+items[which]+"》");
                    }
                });
                //为AlertDialog.Builder添加"确定"按钮
                setPositiveButton(builder);
                //为AlertDialog.Builder添加“取消”按钮
                setNegativeButton(builder)
                        .create()
                        .show();
    }
//3.单选列表项对话框
    public void singleChoice(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                //设置对话框标题
                .setTitle("单选列表项对话框")
                //设置图标
                .setIcon(R.drawable.tools)
                //设置单选列表项，默认选中第二项（索引为1）
                .setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        show.setText("您选中了《" + items[which] + "》");
                    }
                });
        //为AlertDialog.Builder添加"确定"按钮
        setPositiveButton(builder);
        //为AlertDialog.Builder添加“取消”按钮
        setNegativeButton(builder)
                .create()
                .show();
    }

//4.多选列表对话框
    public void multiChoice(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                //设置对话框标题
                .setTitle("多选列表对话框")
                //设置图标
                .setIcon(R.drawable.tools)
                //设置多选列表项，设置勾选第二项、第四项
                .setMultiChoiceItems(items, new boolean[]{false,true,true,true},null);
                //为AlertDialog.Builder添加"确定"按钮
                setPositiveButton(builder);
                //为AlertDialog.Builder添加“取消”按钮
                setNegativeButton(builder)
                        .create()
                        .show();
    }
//5.自定义列表对话框
    public void customList(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                //设置对话框标题
                .setTitle("自定义列表对话框")
                //设置图标
                .setIcon(R.drawable.tools)
                .setAdapter(new ArrayAdapter<String>(this,R.layout.array_item,items),null);
                //为AlertDialog.Builder添加"确定"按钮
                setPositiveButton(builder);
                //为AlertDialog.Builder添加“取消”按钮
                setNegativeButton(builder)
                .create()
                .show();
    }
//6.自定义View对话框
    public void customView(View source) {
        TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(R.layout.login,null);
        new AlertDialog.Builder(this)
                //设置对话框标题
                .setTitle("自定义View对话框")
                //设置图标
                .setIcon(R.drawable.tools)
                .setView(loginForm)
                //为AlertDialog.Builder添加"确定"按钮
                .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //此处可执行登录处理
                    }
                })
                //为AlertDialog.Builder添加“取消”按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //取消登录不做任何处理
                    }
                })
                //创建并显示对话框
                .create()
                .show();
    }

    private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
        //调用setNegativeButton方法添加“取消”按钮
        return  builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                show.setText("单击了【取消】按钮");
            }
        });
    }

    private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
        //调用setPositiveButton方法添加“确定”按钮
        return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                show.setText("单击了【确定】按钮！");
            }
        });
    }
}
